package com.lsege.controller.backstage;


import com.github.pagehelper.PageHelper;
import com.lsege.entity.JsonResult;
import com.lsege.entity.Page;
import com.lsege.entity.backstage.Activity;
import com.lsege.entity.backstage.Prize;
import com.lsege.service.backstage.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Administrator on 2017/7/8.
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    ActivityService activityService;
    /**
     * 活动开启
     * */
    @PostMapping("startActivity")
    public JsonResult startActivity(Activity activity){
        JsonResult json = new JsonResult();
        /*根据活动标题和奖品判断此活动是否已经开启*/
        Long isStart = activityService.queryStatusByTitle(activity);
        if(isStart == 1){
            json.setSuccess(false);
            json.setMessage("此活动已经开启!");
        } else {
            activityService.startActivity(activity);
            json.setSuccess(true);
            json.setMessage("活动开启成功!");
        }
        return json;
    }
    /**
     * 活动关闭
     * */
    @PostMapping("endActivity")
    public JsonResult endActivity(Activity activity){
        JsonResult json = new JsonResult();
         /*根据活动标题和奖品判断此活动是否已经开启*/
         Long isStart = activityService.queryStatusByTitle(activity);
         if(isStart == 1){
             activityService.endActivity(activity);
             json.setSuccess(true);
             json.setMessage("活动成功关闭");
         } else {
             json.setSuccess(false);
             json.setMessage("活动尚未开启");
         }
        return json;
    }
    /**
     * 查询所有活动
     * */
    @GetMapping("getActivityList")
    public JsonResult getActivityList(Integer pageNum,Integer pageSize){
        JsonResult json = new JsonResult();
        PageHelper.startPage(pageNum,pageSize);
        List<Activity> list = activityService.queryActivityList();
        Long total = activityService.queryActivityCount();
        json.setData(new Page(list,total,pageSize,pageNum));
        json.setSuccess(true);
        json.setMessage("获取成功");
        return json;
    }
}
